package com.ysstech.reportworld.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.TableDetail;
import com.ysstech.reportworld.entity.TableDetailStandardCheck;
import com.ysstech.reportworld.entity.TableDetailStandardCheck;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.service.TableDetailStandardCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报表世界字段校验规则管理 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-04-28
 */
@Controller
@Slf4j
@Api(tags = "报表世界字段校验规则管理管理api")
@RequestMapping("/tableDetailStandardCheck")
public class TableDetailStandardCheckController extends BaseController {

    @Autowired
    private TableDetailStandardCheckService tableDetailStandardCheckService;

    /**
     * 跳转字段校验规则管理信息页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:table:detail:standard:check"})
    @ApiOperation(value = "跳转字段校验规则管理信息页面")
    @YssLog(module = "字段校验规则管理信息管理", remark = "跳转字段校验规则管理信息页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "reportworld/tableDetailStandardCheck";
    }


    @YssPermissions(value = {"reportworld:table:detail:standard:check"})
    @ApiOperation(value = "获取字段校验规则管理页面初始值")
    @YssLog(module = "字段校验规则管理", remark = "获取字段校验规则管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 状态
        List<Dicvalue> listStatus = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_STATUS.getCode());
        map.put("listStatus", listStatus);
        map.put("listStatus2", listStatus);
        //字段类型
        List<Dicvalue> listColumnType = dicValueService.getCacheDicValueList(ReportWorldEnum.COLUMN_TYPE.getCode());
        map.put("listColumnType", listColumnType);
        map.put("listColumnType2", listColumnType);
        return ResultGenerator.getSuccessResult("获取字段校验规则管理页面初始值成功", map);
    }

    /**
     * 需求BUG列表查询
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:detail:standard:check"})
    @ApiOperation(value = "获取所有字段校验规则")
    @YssLog(module = "字段校验规则管理", remark = "获取所有字段校验规则")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(TableDetailStandardCheck tableDetailStandardCheck, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取所有字段校验规则成功",
                tableDetailStandardCheckService.queryAllTableDetailStandardCheck(tableDetailStandardCheck, this.getPage(page, rows)));
    }

    /**
     * 新增表字段校验规则信息的方法
     *
     * @param tableDetailStandardCheck
     * @return
     */
    @YssPermissions(value = {"reportworld:table:detail:standard:check:add"})
    @ApiOperation(value = "新增表字段校验规则信息")
    @YssLog(module = "表字段校验规则信息管理", remark = "新增表字段校验规则信息")
    @RequestMapping(value = "/addTableDetailStandardCheck")
    @ResponseBody
    public RestResult addTableDetailStandardCheck(TableDetailStandardCheck tableDetailStandardCheck) throws Exception {
        if (tableDetailStandardCheck == null) {
            throw new BusinessException("获取表字段校验规则信息失败，请稍后重试！");
        }
        tableDetailStandardCheck.setId(DateUtil.generateUUID());
        tableDetailStandardCheck.setAddTime(LocalDateTime.now());
        tableDetailStandardCheck.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableDetailStandardCheckService.save(tableDetailStandardCheck);
        return ResultGenerator.getSuccessResult("新增表字段校验规则信息成功！");
    }

    /**
     * 修改表字段校验规则信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:detail:standard:check:edit"})
    @ApiOperation(value = "修改表字段校验规则信息")
    @YssLog(module = "表字段校验规则信息管理", remark = "修改表字段校验规则信息")
    @RequestMapping(value = "/editTableDetailStandardCheck")
    @ResponseBody
    public RestResult editTableDetailStandardCheck(TableDetailStandardCheck tableDetailStandardCheck) throws Exception {
        if (tableDetailStandardCheck == null || StringUtils.isBlank(tableDetailStandardCheck.getId())) {
            throw new BusinessException("获取表字段校验规则信息失败，请稍后重试！");
        }
        tableDetailStandardCheck.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableDetailStandardCheck.setEditTime(LocalDateTime.now());
        tableDetailStandardCheckService.updateById(tableDetailStandardCheck);
        return ResultGenerator.getSuccessResult("修改表字段校验规则信息成功！");
    }

    /**
     * 删除表字段校验规则信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:detail:standard:check:del"})
    @ApiOperation(value = "删除表字段校验规则信息")
    @YssLog(module = "表字段校验规则信息管理", remark = "删除表字段校验规则信息")
    @RequestMapping(value = "/delTableDetailStandardCheck")
    @ResponseBody
    public RestResult delTableDetailStandardCheck(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除用例信息失败，请稍后重试！");
        }
        List<String> tableDetailStandardCheckIds = JSONArray.parseArray(ids, String.class);
        if (!CollectionUtils.isEmpty(tableDetailStandardCheckIds)) {
            tableDetailStandardCheckService.removeByIds(tableDetailStandardCheckIds);
        }
        return ResultGenerator.getSuccessResult("删除表字段校验规则信息成功！");
    }


    /**
     * 判断用户登录名称是否已经存在
     *
     * @param tableDetailStandardCheck
     * @return
     */
    @ApiOperation(value = "检查表字段名是否存在")
    @YssLog(module = "表字段校验规则信息管理", remark = "检查表名字段是否存在")
    @RequestMapping(value = "/checkColumnName")
    @ResponseBody
    public RestResult checkColumnName(TableDetailStandardCheck tableDetailStandardCheck) {
        if (null == tableDetailStandardCheck || StringUtils.isBlank(tableDetailStandardCheck.getColumnName())) {
            return ResultGenerator.getFailResult("检查用表字段信息失败，请稍后重试！");
        }
        QueryWrapper<TableDetailStandardCheck> wrapper = new QueryWrapper<>();
        wrapper.eq("COLUMN_NAME", tableDetailStandardCheck.getColumnName().toUpperCase());
        int count = tableDetailStandardCheckService.count(wrapper);
        //查询是否存在code值得参数
        if (count > 0) {//返回true表示已经存在
            return ResultGenerator.getSuccessResult("检查表字段信息成功", false);
        } else {
            return ResultGenerator.getSuccessResult("检查表字段信息成功", true);
        }
    }


}
